var LocalStorage = LocalStorage || {};

var LocalStorage = {
	supportLocalStorage : (typeof(Storage) !== "undefined"),
	has : function(name) {
		if (this.supportLocalStorage) {
			return localStorage.getItem(name) != null;
		} else {
			return this.getCookie(name) != null;
		}
	},
	get : function(name) {
		if (this.supportLocalStorage) {
			return localStorage.getItem(name);
		} else {
			return this.getCookie(name);
		}
	},
	getCookie : function(name) {
		var result = null;
		var cookie;
		var c_name;
		var c_value;
		var cookies = document.cookie.split(";");
		for (var i = 0; i < cookies.length; ++i) {
			cookie = cookies[i];
			c_name = cookie.substr(0, cookie.indexOf("="));
			c_value = cookie.substr(cookie.indexOf("=") + 1);
			c_name = c_name.replace(/^\s+|\s+$/g, "");
			if (c_name == name) {
				result = unescape(c_value);
				break;
			}
		}
		return result;
	},
	set : function(name, value) {
		if (this.supportLocalStorage) {
			localStorage.setItem(name, value);
		} else {
			this.setCookie(name, value, 365);
		}
	},
	setCookie : function(name, value, exdays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_name = name.replace(/^\s+|\s+$/g, "");
		var c_value = escape(value) + ((exdays == null) ? "" : "; expires = " + exdate.toUTCString());
		document.cookie = c_name + "=" + c_value;
	},
	del : function(name) {
		if (this.supportLocalStorage) {
			localStorage.removeItem(name);
		} else {
			this.setCookie(name, "", -1);
		}
	},
	delCookie : function(name) {
		var result;
		var c_name;
		var c_value;
		var cookies = document.cookie.split(";");
		for (var i = 0; i < cookies.length; ++i) {
			cookie = cookies[i];
			c_name = cookie.substr(0, cookie.indexOf("="));
			c_value = cookie.substr(cookie.indexOf("=") + 1);
			if (c_name == name) {
				cookies.splice(i, 1);
				result = c_value;
				break;
			}
		}
		return result;
	},
};
